// src/utils/timeUtils/DateTime.js  
import { ref, onMounted, onBeforeUnmount } from 'vue'
 
export function useDateTime() {
  const currentDate = ref('')
  const currentTime = ref('')
 
  const updateTime = () => {
    const now = new Date()
    currentDate.value  = now.toLocaleDateString('zh-CN',  {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    }).replace(/\//g, '-')
    currentTime.value  = now.toLocaleTimeString('zh-CN',  {
      hour12: false,
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  }
 
  // 关键：在 composable 中初始化定时器 
  onMounted(() => {
    updateTime()
    const timer = setInterval(updateTime, 1000)
    onBeforeUnmount(() => clearInterval(timer))
  })
 
  return { currentDate, currentTime }
}